package com.cictec.middleware.gps.engine.position.handler;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.core.entity.BusLineStation;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.TimingPoint;
import com.cictec.middleware.gps.engine.exception.BusPositionHandlerException;
import com.cictec.middleware.gps.engine.position.BusPositionHandler;
import com.cictec.middleware.gps.service.BusLineService;

/**
 * 站点信息处理
 * @author ql
 * @date 2017年9月26日
 */
@Component
public class BusPositionStationHandler implements BusPositionHandler {

	private Logger logger = LoggerFactory.getLogger(BusPositionStationHandler.class);
	
	@Autowired
	private BusLineService busLineService;
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionHandlerException {
		logger.debug("【进出站、站点信息】");
//		if(busInstance.isInOperateTime() && busInstance.isLineTypeExist()){
		if(busInstance.isLineTypeExist()){
			getStation(busInstance);
		}
		
		logger.debug("\t【进出站、站点信息】站点名称：{}", busInstance.getStationName());
		logger.debug("\t【进出站、站点信息】站点序号：{}", busInstance.getStationSeq());
		logger.debug("\t【进出站、站点信息】站点进出站状态：{}", busInstance.getStationEnterOut());
		
		
	}

	/**
	 * 获取站点信息
	 * @param busInstance
	 */
	private void getStation(BusInstance busInstance) {
		
		if (!busInstance.isEnterOut()) {
			
			TimingPoint currentStation = busInstance.getCurrentStation();
			
			if(busInstance.isInStandBy()){
				// 待发区
				// 待发区 取 上行或下行待发区第一站
				BusLineStation lineStation = busLineService.getLineStationFirst(busInstance.getLineUuid(), busInstance.getLineType());
				if(lineStation != null){
					busInstance.setStationSeq(lineStation.getLsSequence()+"");
					busInstance.setStationName(lineStation.getStaName());
					busInstance.setStationUuid(lineStation.getLsUuid());
				}
			}else if(currentStation != null){
				
				busInstance.setStationSeq(currentStation.getStationSeq());
				busInstance.setStationName(currentStation.getStationName());
				busInstance.setStationEnterOut(currentStation.getStationEnterOut());
				busInstance.setStationUuid(currentStation.getStationUuid());
			}else{
				// 服务器判定
				if(!busInstance.isInDeviate()){
//					setStationByPosition(busInstance);
				}
			}
		}
	}
	
	/**
	 * 根据位置获取站点信息
	 */
	private void setStationByPosition(BusInstance busInstance) {
		
		String lineUuid = busInstance.getLineUuid();
		String lineType = busInstance.getLineType();
		String latitude = busInstance.getLatitude();
		String longitude = busInstance.getLongitude();
		
		
		BusLineStation lineStation = null;
		// 查找车辆所在站点，如果在站点内，则判断为进站数据
		List<BusLineStation> currentInStations = busLineService.getLineStationByPosition(lineUuid, lineType, latitude, latitude);
		// 车辆在站内 进站状态
		if (currentInStations.size() > 0) {
			// 【进站状态】如果多个 添加方向角区分，暂时没做
			
			lineStation = currentInStations.get(0);
			busInstance.setStationSeq(PMSUtils.isNull(lineStation.getLsSequence()));
			busInstance.setStationEnterOut(Constants.VAL_ENTER_OUT_SATION_ENTER);
			busInstance.setStationName(lineStation.getStaName());
			busInstance.setStationUuid(lineStation.getLsStaUuid());
		}else{
			// 【出站状态】
			lineStation = busLineService.getNearstBusLineStation(lineUuid, lineType, latitude, longitude);
			if(lineStation != null){
				busInstance.setStationSeq(PMSUtils.isNull(lineStation.getLsSequence()));
				busInstance.setStationName(lineStation.getStaName());
				busInstance.setStationEnterOut(Constants.VAL_ENTER_OUT_SATION_OUT);
				busInstance.setStationUuid(lineStation.getLsStaUuid());
			}
		}
		
		
	}

}
